package com.personal.system.controller.approval;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.personal.common.result.Result;
import com.personal.system.domain.CostQuoteRules;
import com.personal.system.service.approve.CostQuoteRulesService;
import com.personal.system.config.SecurityUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/rules")
public class CostQuoteRulesController {

    @Resource
    private CostQuoteRulesService costQuoteRulesService;
    @Resource
    private SecurityUtil securityUtil;

    /**
     * 查询成本报价规则管理列表
     */
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Result list(@PathVariable int pageNum, @PathVariable int pageSize) {
        Page<CostQuoteRules> pageParam = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<CostQuoteRules> qw = new LambdaQueryWrapper<>();
        qw.orderByDesc(CostQuoteRules::getCreateTime);
        Page<CostQuoteRules> list = costQuoteRulesService.page(pageParam,qw);

        return Result.ok(list);
    }

    @PostMapping("/addCost")
    public Result addCost(@RequestBody CostQuoteRules quoteRules) throws Exception {

        costQuoteRulesService.addCost(quoteRules);
        return Result.ok();
    }

    @GetMapping("/getCost/{ruleId}")
    public Result addCost(@PathVariable String ruleId) {

        return Result.ok(costQuoteRulesService.getById(ruleId));
    }

    @PostMapping("/delCost")
    public Result delCost(@RequestBody List<CostQuoteRules> ruleIds) {

        return Result.ok(costQuoteRulesService.removeByIds(ruleIds.stream().map(e -> e.getRuleId()).collect(Collectors.toList())));
    }

    @PostMapping("/submitApprove")
    public Result submitApprove(@RequestBody List<CostQuoteRules> ids) {

        costQuoteRulesService.submitApprove(ids);

        return Result.ok();
    }

    /**
     * 查询待审列表
     */
    @GetMapping("/getPendingList/{pageNum}/{pageSize}")
    public Result getPendingList(@PathVariable int pageNum, @PathVariable int pageSize) {
        Page<CostQuoteRules> pageParam = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<CostQuoteRules> qw = new LambdaQueryWrapper<>();
        qw.eq(CostQuoteRules::getRuleState, "02");
        qw.eq(CostQuoteRules::getAuditType, "01");
        qw.eq(CostQuoteRules::getAuditUserId, securityUtil.getUserId());
        // 角色审 或 部门审 换个类型
//        qw.or();
//        qw.eq(CostQuoteRules::getAuditType, "02");
//        qw.in(CostQuoteRules::getAuditUserId, new ArrayList<>());

        Page<CostQuoteRules> list = costQuoteRulesService.page(pageParam, qw);

        return Result.ok(list);
    }

    /**
     * 提交审核
     */
    @PostMapping("/approve")
    public Result approve(@RequestBody Map<String,Object> params) {

        costQuoteRulesService.approve(params);
        return Result.ok();
    }
    /**
     * 加签功能
     */
    @PostMapping("/addSign")
    public Result addSign(@RequestBody Map<String,Object> params) {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) attributes).getRequest();
        costQuoteRulesService.addSign(params);
        return Result.ok();
    }

    /**
     * 查看审批
     */
    @GetMapping("/findApproval/{ruleId}")
    public Result findApproval(@PathVariable String ruleId) {

        return Result.ok(costQuoteRulesService.findApproval(ruleId));
    }

}
